package com.pie4cloud.pie.job.api.consumer.factory;


import com.pie4cloud.pie.job.api.consumer.IHanlderConsumer;
import com.pie4cloud.pie.job.api.consumer.annotation.Hanlder;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class IHanlderFactory implements ApplicationContextAware, DisposableBean {
	private ConcurrentHashMap<String, IHanlderConsumer> services=new ConcurrentHashMap<>();
	@Override
	public void destroy() throws Exception {

	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		//ConcurrentHashMap<String, IHanlderConsumer> services=new ConcurrentHashMap<>();
		Map<String, Object> serviceMap = applicationContext.getBeansWithAnnotation(Hanlder.class);
		if (serviceMap!=null && serviceMap.size()>0) {
			for (Object serviceBean : serviceMap.values()) {
				if (serviceBean instanceof IHanlderConsumer) {
					IHanlderConsumer iHanlderConsumer= (IHanlderConsumer) serviceBean;
					Hanlder annotation = iHanlderConsumer.getClass().getAnnotation(Hanlder.class);
					services.put(annotation.handler(),iHanlderConsumer);
				}
			}
		}
		//HanlderClientFactory.services.putAll(services);
	}
	public IHanlderConsumer get(String serviceName){
		return services.get(serviceName);
	}

}
